<script setup lang="ts">
import {reactive, ref} from 'vue'

//定义参数类型
interface FormState {
  username: string
  password: number
}

const formRef = ref()
const form = reactive({
  username: '',
  password: ''
})
const rules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur'}
  ]
}
const submitForm = (() => {
  formRef.value.validate((valid: any) => {
    if (valid) {
      console.log('submit!')
    } else {
      console.log('error')
    }
  })
})
const resetForm = (() => {
  formRef.value.resetFields()
})


/**
 * js中简单的使用class
 * */
class myClass {
  name: string | undefined
  age: number | undefined
  nameClass: string | undefined
  ageSetting: any

  constructor(name: any, age: any, nameClass: any, ageSetting: any) {
    this.name = name
    this.age = age
    this.nameClass = nameClass
    this.ageSetting = ageSetting
  }

  sayHello() {
    console.log('您好!')
  }

  sayName() {
    console.log('我得名字是:' + this.name, this.age, this.nameClass, this.ageSetting)
  }

  submitForm() {
    const div = document.createElement('div')
    div.textContent = this.name + `${this.age}` + this.nameClass + this.ageSetting
    document.body.appendChild(div)
  }
}

const ps = new myClass('小明', 27, '设置class', '年龄设置')


ps.sayHello()
ps.sayName()
ps.submitForm()

</script>

<template>
  <div class="from-main">
    <el-card style="width: 480px" shadow="always">
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="submitForm">提 交</el-button>
          <el-button @click="resetForm">重 置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
//居中布局
.from-main {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
</style>
